package cn.com.haopy.yue.ai.wx.web.base;


import cn.com.haopy.yue.ai.db.entity.arisk.HOSUser;
import cn.com.haopy.yue.ai.db.service.hos.HOSUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

/**
 * 测试服务
 */
@RestController
@RequiredArgsConstructor
@Slf4j
public class BaseWxController {

    protected HttpServletRequest request;
    protected HttpServletResponse response;

    @Autowired
    HOSUserService hosUserService;

    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) throws Exception {
        this.request = request;
        this.response = response;
        this.request.setCharacterEncoding("UTF-8");
        this.response.setCharacterEncoding("UTF-8");
    }

    public TreeMap<String, String> getParams(HttpServletRequest request) {
        TreeMap<String, String> map = new TreeMap<String, String>();
        Map reqMap = request.getParameterMap();
        for (Object key : reqMap.keySet()) {
            String value = ((String[]) reqMap.get(key))[0];
            System.out.println(key + ";" + value);
            map.put(key.toString(), value);
        }
        return map;
    }

    protected HOSUser getLoginUser(Integer userId) {
        if (Objects.nonNull(userId)) {
            HOSUser hosUser = hosUserService.getById(userId);
            return hosUser;
        }
        return null;
    }

}